home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / nspipe.s5 / readnspipe.c < prev   
C/C++ Source or Header  |  1989-12-17  |  940b  |  46 lines

  1. /*
  2.  * Create a named stream pipe and read from it.
  3.  */
  4.  
  5. #include    <sys/types.h>
  6. #include    <stropts.h>
  7.  
  8. #define    NSPIPENAME    "/tmp/nspipe.serv"
  9. #define    BUFFSIZE    1024
  10.  
  11. main()
  12. {
  13.     int        fd[2], flags;
  14.     char        cntlbuff[BUFFSIZE], databuff[BUFFSIZE];
  15.     struct strbuf    cntlstr, datastr;
  16.  
  17.     if (ns_pipe(NSPIPENAME, fd) < 0)
  18.         err_sys("can't create named stream pipe");
  19.  
  20.     cntlstr.buf    = cntlbuff;
  21.     cntlstr.maxlen = BUFFSIZE;
  22.     cntlstr.len    = 0;
  23.  
  24.     datastr.buf    = databuff;
  25.     datastr.maxlen = BUFFSIZE;
  26.     datastr.len    = 0;
  27.  
  28.     flags = 0;
  29.  
  30.     for ( ; ; ) {
  31.         /*
  32.          * Since the ns_pipe() function associates the name with
  33.          * fd[0], we have to read from fd[1] (the other end of
  34.          * the pipe).
  35.          */
  36.  
  37.         if (getmsg(fd[1], &cntlstr, &datastr, &flags) < 0)
  38.             err_sys("getmsg error");
  39.         if (cntlstr.len > 0)
  40.             printf("received %d bytes of control information\n",
  41.                     cntlstr.len);
  42.         if (datastr.len > 0)
  43.             printf("data: %.*s\n", datastr.len, datastr.buf);
  44.     }
  45. }
  46.